package com.dev.print.protocol;


import com.dev.DevPosix;
import com.dev.print.protocol.base.SPConfig;

import java.io.File;


public class ReadWriteFrame {
	private SPConfig config = null;;
	
	public boolean registerSP(SPConfig paramConfig){
		config = paramConfig;
		if(null != config && null != config.getPath() && !"".equals(config.getPath()) && new File(config.getPath()).exists() && config.getFlages() >= 0 && config.getSpeed() > 0){
			if(!isCanRW())
				if(!chmod())
					return false;
			config.setmFd(openSP());
        if(config.getmFd() > 0)
			return true;	
		}return false;
	}
	
	public byte[] readRtcFrame(){
		return DevPosix.read(config.getmFd());
	}
	
	public int writeRtcFrame(byte[] buffer){
		return DevPosix.write(config.getmFd(), buffer, buffer.length);
	}
	
	public boolean chmod(){
		return 0 == DevPosix.chmod(config.getPath(),777);//获取权限;
	}
	
	public int openSP(){
		return DevPosix.openSP(config.getPath(), config.getSpeed(), config.getFlages());
	}
	
	public boolean isCanRW(){
		File mFile = null;
		if(null == (mFile = new File(config.getPath())) || !mFile.exists() || !mFile.canWrite() || !mFile.canWrite())
			return false;
		return true;
	}
}
